package com.sds.sweng.swat.miplatform.ui.design.parts.area.sub;

import java.util.List;

import org.eclipse.draw2d.GridLayout;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.ScrollPane;
import org.eclipse.draw2d.Viewport;

import com.sds.sweng.swat.miplatform.ui.design.figures.MLabel;
import com.sds.sweng.swat.miplatform.ui.design.figures.logical.LGridCellFigure;
import com.sds.sweng.swat.miplatform.ui.design.layouts.EasyGridData;
import com.sds.sweng.swat.miplatform.ui.design.layouts.EasyGridLayout;
import com.sds.sweng.swat.miplatform.ui.design.models.area.sub.GridSubArea;
import com.sds.sweng.swat.miplatform.ui.design.models.component.MColumn;
import com.sds.sweng.swat.miplatform.ui.design.models.component.MGrid;
import com.sds.sweng.swat.miplatform.ui.design.models.component.MGridBody;
import com.sds.sweng.swat.miplatform.ui.design.models.component.MGridCell;
import com.sds.sweng.swat.miplatform.ui.design.models.component.MGridHead;

public class GridSubAreaPart extends SubAreaPart {

	protected IFigure createFigure() {
		ScrollPane f = new ScrollPane();
		f.setBorder(new LineBorder());
		f.setScrollBarVisibility(ScrollPane.AUTOMATIC);
		f.setViewport(new Viewport());

		Label contents = new Label();
		f.setContents(contents);

		drawChildren(contents);

		int subAreaWidth = getAreaWidth();
		f.setSize(subAreaWidth, GridSubArea.MODEL_HEIGHT);

		return f;
	}

	private void drawChildren(Label parent) {
		clearChildren(parent);

		GridSubArea area = (GridSubArea) getModel();

		MGrid grid = area.getGrid();
		List<MColumn> columns = grid.getColumns();
		List<MGridHead> gridHeads = grid.getGridHeads();
		List<MGridBody> gridBodys = grid.getGridBodys();

		int numColumns = columns.size();

		GridLayout layout = EasyGridLayout.create(numColumns, false, 0, 0, 0, 0);
		parent.setLayoutManager(layout);

		int headHeight = 0;
		int bodyHeight = 0;

		for (int i=0;i<gridHeads.size();i++) {
			MGridHead gridHead = gridHeads.get(i);
			LGridCellFigure headFigure = new LGridCellFigure(LGridCellFigure.TYPE_HEAD, gridHead.getText(), null, gridHead.getDisplay(), null);

			int width = columns.get(gridHead.getCol()).getWidth();
			for (int j=1;j<gridHead.getColspan();j++)
				width += columns.get(gridHead.getCol()+j).getWidth();

			int height = MGridHead.DEFAULT_HEIGHT;
			for (int j=1;j<gridHead.getRowspan();j++)
				height += MGridHead.DEFAULT_HEIGHT;

			headHeight = Math.max(headHeight, MGridHead.DEFAULT_HEIGHT*(gridHead.getRow()+gridHead.getRowspan()));

			headFigure.setSize(width, height);

			if (MGridCell.HALIGN_LEFT.equals(gridHead.getHalign())) {
				headFigure.setAlignment(MLabel.ALIGN_LEFT);
			} else if (MGridCell.HALIGN_CENTER.equals(gridHead.getHalign())) {
				headFigure.setAlignment(MLabel.ALIGN_CENTER);
			} else if (MGridCell.HALIGN_RIGHT.equals(gridHead.getHalign())) {
				headFigure.setAlignment(MLabel.ALIGN_RIGHT);
			}

			if (MGridCell.VALIGN_TOP.equals(gridHead.getValign())) {
				headFigure.setVerticalAlignment(MLabel.VALIGN_TOP);
			} else if (MGridCell.VALIGN_MIDDLE.equals(gridHead.getValign())) {
				headFigure.setVerticalAlignment(MLabel.VALIGN_MIDDLE);
			} else if (MGridCell.VALIGN_BOTTOM.equals(gridHead.getHalign())) {
				headFigure.setVerticalAlignment(MLabel.VALIGN_BOTTOM);
			}

			parent.add(headFigure);
			layout.setConstraint(headFigure, EasyGridData.create(width, height, gridHead.getColspan(), gridHead.getRowspan()));
		}

		for (int i=0;i<gridBodys.size();i++) {
			MGridBody gridBody = gridBodys.get(i);
			LGridCellFigure bodyFigure = new LGridCellFigure(LGridCellFigure.TYPE_BODY, gridBody.getColid(), gridBody.getFormat(), gridBody.getDisplay(), gridBody.getEdit());

			int width = columns.get(gridBody.getCol()).getWidth();
			for (int j=1;j<gridBody.getColspan();j++)
				width += columns.get(gridBody.getCol()+j).getWidth();

			int height = MGridBody.DEFAULT_HEIGHT;
			for (int j=1;j<gridBody.getRowspan();j++)
				height += MGridBody.DEFAULT_HEIGHT;

			bodyHeight = Math.max(bodyHeight, MGridBody.DEFAULT_HEIGHT*(gridBody.getRow()+gridBody.getRowspan()));

			bodyFigure.setSize(width, height);

			if (MGridCell.HALIGN_LEFT.equals(gridBody.getHalign())) {
				bodyFigure.setAlignment(MLabel.ALIGN_LEFT);
			} else if (MGridCell.HALIGN_CENTER.equals(gridBody.getHalign())) {
				bodyFigure.setAlignment(MLabel.ALIGN_CENTER);
			} else if (MGridCell.HALIGN_RIGHT.equals(gridBody.getHalign())) {
				bodyFigure.setAlignment(MLabel.ALIGN_RIGHT);
			}

			if (MGridCell.VALIGN_TOP.equals(gridBody.getValign())) {
				bodyFigure.setVerticalAlignment(MLabel.VALIGN_TOP);
			} else if (MGridCell.VALIGN_MIDDLE.equals(gridBody.getValign())) {
				bodyFigure.setVerticalAlignment(MLabel.VALIGN_MIDDLE);
			} else if (MGridCell.VALIGN_BOTTOM.equals(gridBody.getHalign())) {
				bodyFigure.setVerticalAlignment(MLabel.VALIGN_BOTTOM);
			}

			parent.add(bodyFigure);
			layout.setConstraint(bodyFigure, EasyGridData.create(width, height, gridBody.getColspan(), gridBody.getRowspan()));
		}

		int gridHeight = headHeight + bodyHeight*grid.getLines();
		grid.setHeight(gridHeight);
	}

	protected void refreshVisuals() {
		drawChildren((Label) ((ScrollPane) getFigure()).getContents());
	}

}
